package com.sn.core.util.pub.tool;

import com.alibaba.fastjson.JSONObject;
import com.sn.core.util.AesTools;
import com.sn.core.http.CommResult;
import com.sn.core.util.pub.exception.DBExceptionEnums;
import com.sn.core.util.pub.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


public class ResponseTools {
    private static ResponseTools tool;
    private Logger logger;

    public ResponseTools() {
        this.logger = LoggerFactory.getLogger(super.getClass());
    }

    public static ResponseTools get() {
        if (tool == null)
            tool = new ResponseTools();
        return tool;
    }

    private CommResult buildBizInfo(Object info) {
        CommResult result = new CommResult();
        result.setSuccess(true);
        result.setErrmsg("success");
        result.setErrcode("6666");
        if(info==null)
            result.setData("");
        else
        result.setData(info);
        result.setNow(System.currentTimeMillis());
        return result;
    }
    
    /**
     * 
     * @param info
     * @param key
     * @return
     */
    private CommResult buildBIzInfoWithAuth(Object info, String key){

        if (info == null) {
            info = new Object();
        }
        CommResult result = new CommResult();
        result.setSuccess(true);
        result.setErrmsg("success");
        result.setErrcode("6666");
        String r=JSONObject.toJSONString(info);
        Long a=System.currentTimeMillis();
        String er="";
        try {
             er = AesTools.encryptAES(r, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // String encryptInfo = AesUtil.encode(r,encrtyKey);
        //String aa=AesUtil.decode(encryptInfo,encrtyKey);
       // String o=TeaUtil.decryptB  yTea(encryptInfo);
        Long b =System.currentTimeMillis();
        Long c=b-a;
        result.setData(er);
        result.setCrpt(1);
        return result;

    }

    public void responseErrResult(HttpServletResponse response,Object o)
    {
        CommResult result=response(o);
        String json= JSONObject.toJSONString(result);
        responseResult(response,json);
    }
    
    private CommResult responseInfoWithAuth(Object info, String key){
        return buildBIzInfoWithAuth(info, key);
    }
    
    public CommResult responseWithAuth(Object info, String key)
    {
        return responseInfoWithAuth(info, key);
    }


    public void responseResult(HttpServletResponse resp, String o) {
        PrintWriter pw = null;
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=utf-8");
        String value = o;
        try {
            pw = resp.getWriter();
            pw.write(value.toString());
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            pw.close();
        }
    }

    private CommResult buildError(ServiceException e) {

        CommResult result = new CommResult();
        result.setSuccess(false);
        result.setErrmsg(e.getMessage());
        result.setErrcode(e.getCode());
        result.setData(null);
        result.setNow(System.currentTimeMillis());
        return result;
    }

    private CommResult buildExceError(Exception e) {

        CommResult result = new CommResult();
        result.setSuccess(false);
        result.setErrmsg(e.getMessage());
        result.setErrcode(DBExceptionEnums.SYSTEM__ERROR.getCode());
        result.setData(null);
        result.setNow(System.currentTimeMillis());
        return result;
    }

    private CommResult responseInfo(Object info) {
        return buildBizInfo(info);
    }

    public void response(Object info, Object page) {
        if (info instanceof ServiceException)
            responseError((ServiceException) info, null);
        else if (info instanceof Exception)
            responseError(null, (Exception) info);
        else
            responseInfo(info);
    }

    public CommResult response(Object info) {
        if (info instanceof ServiceException)
            return responseError((ServiceException) info, null);
        else if (info instanceof Exception)
            return responseError(null, (Exception) info);
        else
            return responseInfo(info);
    }

    private CommResult responseError(ServiceException e,
                                 Exception e1) {
    	CommResult obj = null;
        if (e != null)
            obj = buildError(e);
        else
            obj = buildExceError(e1);

        return obj;
    }
}